package ac.you.api.nms;

import ac.you.api.AcYouAPI;
import org.bukkit.Bukkit;

import java.lang.reflect.Method;
import java.util.Objects;

public final class NMSApi {
    public static String convertNMSVersion(){
        return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
    }
    public static Class findNMSPackage(String className){
        Objects.requireNonNull(className);
        if (AcYouAPI.NMS_Version != null){
            try {
                return Class.forName(Package.NMS.a() + "." + AcYouAPI.NMS_Version + "." + className);
            }catch (ClassNotFoundException ignored){}
        }
        return null;
    }
    public static Class findCraftPackage(String className)  {
        Objects.requireNonNull(className);
        if (AcYouAPI.NMS_Version != null){
            try {
                return Class.forName(Package.CRAFTBUKKIT.a() + "." + AcYouAPI.NMS_Version + "." +className);
            }catch (ClassNotFoundException ignored){}
        }
        return null;
    }
    public static Object Cast(Class c,Object s){
        Objects.requireNonNull(c);
        Objects.requireNonNull(s);
        if (Package.b(c.getClass().getPackage().getName())){
            if (c.isAssignableFrom(s.getClass())) {
                return c.cast(s);
            }
        }
        return null;
    }
    public static Method method(Class c,String mName,Class... param){
        Objects.requireNonNull(c);
        try {
            if (Package.b(c.getClass().getPackage().getName())){
                Method m = c.getDeclaredMethod(mName, param);
                m.setAccessible(true);
                return m;
            }
        } catch (NoSuchMethodException ignored){}
        return null;
    }
}
